package net.siit.ling;

import java.util.ArrayList;
import java.util.List;
import net.siit.datatype.AnnotatedRange;
import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CharStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import org.antlr.runtime.TokenStream;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.Tree;

/**
 *
 * @author arthit
 */
public class TCCSeg {
    public List<AnnotatedRange<Integer>> clusterize(String text) {
        ANTLRStringStream stream = new ANTLRStringStream(text);
        if (stream == null) {
            return null;
        }
        return clusterize(stream);
    }

    public List<AnnotatedRange<Integer>> clusterize(CharStream stream) {
        TCCLexer lexer   = new TCCLexer(stream);
        TokenStream tokens = new CommonTokenStream(lexer);
        TCCParser parser = new TCCParser(tokens);
        TCCParser.s_return r = null;

        try {
            r = parser.s();
        } catch(RecognitionException e) {
            e.printStackTrace();
            return null;
        }

        List<AnnotatedRange<Integer>> clusters
                = new ArrayList<AnnotatedRange<Integer>>();

        if (r != null) {
            int index = 0;
            CommonTree t = (CommonTree) r.getTree();
            if (t != null) {
                int tokenNum = t.getChildCount();
                for (int i = 0; i < tokenNum; i++) {
                    Tree token = t.getChild(i);
                    AnnotatedRange<Integer> cluster = new AnnotatedRange<Integer>();
                    int type = token.getType();
                    switch(type) {
                    case TCCParser.CLUSTER_TCC:
                        type = ThaiSeg.CLUSTER_LABEL_TCC; break;
                    case TCCParser.CLUSTER_ATCC:
                        type = ThaiSeg.CLUSTER_LABEL_ATCC; break;
                    case TCCParser.CLUSTER_BTCC:
                        type = ThaiSeg.CLUSTER_LABEL_BTCC; break;    
                    case TCCParser.CLUSTER_TDIGITS:
                        type = ThaiSeg.CLUSTER_LABEL_THAI_DIGITS; break;
                    case TCCParser.CLUSTER_ADIGITS:
                        type = ThaiSeg.CLUSTER_LABEL_ARABIC_DIGITS; break;
                    case TCCParser.CLUSTER_SYMBOL:
                        type = ThaiSeg.CLUSTER_LABEL_SYMBOL; break;
                    case TCCParser.CLUSTER_WS:
                        type = ThaiSeg.CLUSTER_LABEL_WHITESPACE; break;
                    case TCCParser.CLUSTER_OTHERS:
                    default:
                        type = ThaiSeg.CLUSTER_LABEL_OTHERS; break;
                    }
                    cluster.setLabel(type);
                    cluster.setStart(index);
                    index = index + token.getChildCount();
                    cluster.setEnd(index);
                    clusters.add(cluster);
                }
            }
        }
        return clusters;
    }

    public List<String> clusterizeAsString(String text) {
        ANTLRStringStream stream = new ANTLRStringStream(text);
        if (stream == null) {
            return null;
        }
        return clusterizeAsString(text);
    }

    public List<String> clusterizeAsString(CharStream stream) {
        TCCLexer lexer   = new TCCLexer(stream);
        TokenStream tokens = new CommonTokenStream(lexer);
        TCCParser parser = new TCCParser(tokens);
        TCCParser.s_return r = null;

        try {
            r = parser.s();
        } catch(RecognitionException e) {
            e.printStackTrace();
            return null;
        }

        List<String> clusters = new ArrayList<String>();
        if (r != null) {
            int index = 0;
            CommonTree t = (CommonTree) r.getTree();
            if (t != null) {
                int tokenNum = t.getChildCount();
                for (int i = 0; i < tokenNum; i++) {
                    int end = index + t.getChild(i).getChildCount();
                    clusters.add(stream.substring(index, end));
                    index = end;
                }
            }
        }
        return clusters;
    }

}
